package com.sunyur.tailor.channel.common.middle.util;

import cn.hutool.json.JSONObject;
import de.odysseus.staxon.json.JsonXMLConfig;
import de.odysseus.staxon.json.JsonXMLConfigBuilder;
import de.odysseus.staxon.json.JsonXMLInputFactory;
import de.odysseus.staxon.json.JsonXMLOutputFactory;
import de.odysseus.staxon.xml.util.PrettyXMLEventWriter;

import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLEventWriter;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLOutputFactory;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StaxonUtils {
/**
 * @program: tailor_channel
 * @description:
 * @author: pengbing
 * @create: 2022-01-06 14:40
 **/
    /**
     * @Description: json string convert to xml string
     * @author watermelon_code
     * @date 2017年7月19日 上午10:50:32
     */
    public static String json2xml(String json) {
        StringReader input = new StringReader(json);
        StringWriter output = new StringWriter();
        JsonXMLConfig config = new JsonXMLConfigBuilder().multiplePI(false).repairingNamespaces(false).build();
        try {
            XMLEventReader reader = new JsonXMLInputFactory(config).createXMLEventReader(input);
            XMLEventWriter writer = XMLOutputFactory.newInstance().createXMLEventWriter(output);
            writer = new PrettyXMLEventWriter(writer);
            writer.add(reader);
            reader.close();
            writer.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                output.close();
                input.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return output.toString();
    }

    /**
     * @Description: json string convert to xml string ewidepay ues only
     * @author watermelon_code
     * @date 2017年7月19日 上午10:50:32
     */
    public static String json2xmlPay(String json) {
        StringReader input = new StringReader(json);
        StringWriter output = new StringWriter();
        JsonXMLConfig config = new JsonXMLConfigBuilder().multiplePI(false).repairingNamespaces(false).build();
        try {
            XMLEventReader reader = new JsonXMLInputFactory(config).createXMLEventReader(input);
            XMLEventWriter writer = XMLOutputFactory.newInstance().createXMLEventWriter(output);
            writer = new PrettyXMLEventWriter(writer);
            writer.add(reader);
            reader.close();
            writer.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                output.close();
                input.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (output.toString().length() >= 38) {// remove <?xml version="1.0" encoding="UTF-8"?>
            return "<xml>" + output.toString().substring(39) + "</xml>";
        }
        return output.toString();
    }

    /**
     * @Description: xml string convert to json string
     * @author watermelon_code
     * @date 2017年7月19日 上午10:50:46
     */
    public static String xml2json(String xml) {
        StringReader input = new StringReader(xml);
        StringWriter output = new StringWriter();
        JsonXMLConfig config = new JsonXMLConfigBuilder().autoArray(true).autoPrimitive(true).prettyPrint(true).build();
        try {
            XMLEventReader reader = XMLInputFactory.newInstance().createXMLEventReader(input);
            XMLEventWriter writer = new JsonXMLOutputFactory(config).createXMLEventWriter(output);
            writer.add(reader);
            reader.close();
            writer.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                output.close();
                input.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return output.toString();
    }

    /**
     * @Description: 去掉转换xml之后的换行和空格
     * @author watermelon_code
     * @date 2017年8月9日 下午4:05:44
     */
    public static String json2xmlReplaceBlank(String json) {
        String str = StaxonUtils.json2xml(json);
        String dest = "";
        if (str != null) {
            Pattern p = Pattern.compile("\\s*|\t|\r|\n");
            Matcher m = p.matcher(str);
            dest = m.replaceAll("");
        }
        return dest;
    }

    public static void main(String[] args) {
        JSONObject json = new JSONObject();
        json.put("name", "jack");
        json.put("age", 25);
        String a = "{\n" +
                "    \"TableList\":{\n" +
                "        \"loginName\":\"zhugenlan\",\n" +
                "        \"tCode\":\"FPC082\",\n" +
                "        \"title\":\"电商采购申请单-EBS-流水号\",\n" +
                "        \"Table\":[\n" +
                "            {\n" +
                "                \"isson\":\"1\",\n" +
                "                \"Field\":[\n" +
                "                    {\n" +
                "                        \"name\":\"流水号\",\n" +
                "                        \"value\":\"PO1234455\"\n" +
                "                    },\n" +
                "                    {\n" +
                "                        \"name\":\"申请时间\",\n" +
                "                        \"value\":\"2021-12-29\"\n" +
                "                    },\n" +
                "                    {\n" +
                "                        \"name\":\"申请事业部\",\n" +
                "                        \"value\":\"采购管理中心\"\n" +
                "                    },\n" +
                "                    {\n" +
                "                        \"name\":\"采购供应商\",\n" +
                "                        \"value\":\"1\"\n" +
                "                    },\n" +
                "                    {\n" +
                "                        \"name\":\"币种\",\n" +
                "                        \"value\":\"CNY\"\n" +
                "                    },\n" +
                "                    {\n" +
                "                        \"name\":\"业务实体\",\n" +
                "                        \"value\":\"OU_SZPCB\"\n" +
                "                    },\n" +
                "                    {\n" +
                "                        \"name\":\"组织ID\",\n" +
                "                        \"value\":\"S01\"\n" +
                "                    }\n" +
                "                ]\n" +
                "            },\n" +
                "            {\n" +
                "                \"isson\":\"2\",\n" +
                "                \"row\":{\n" +
                "                    \"Field\":[\n" +
                "                        {\n" +
                "                            \"name\":\"物料编码\",\n" +
                "                            \"value\":\"1\"\n" +
                "                        },\n" +
                "                        {\n" +
                "                            \"name\":\"物料名称\",\n" +
                "                            \"value\":\"1\"\n" +
                "                        },\n" +
                "                        {\n" +
                "                            \"name\":\"物料类型\",\n" +
                "                            \"value\":\"1\"\n" +
                "                        },\n" +
                "                        {\n" +
                "                            \"name\":\"采购提前期\",\n" +
                "                            \"value\":\"1\"\n" +
                "                        },\n" +
                "                        {\n" +
                "                            \"name\":\"数量\",\n" +
                "                            \"value\":\"1\"\n" +
                "                        },\n" +
                "                        {\n" +
                "                            \"name\":\"单位\",\n" +
                "                            \"value\":\"1\"\n" +
                "                        },\n" +
                "                        {\n" +
                "                            \"name\":\"单价\",\n" +
                "                            \"value\":\"1\"\n" +
                "                        },\n" +
                "                        {\n" +
                "                            \"name\":\"价格对比\",\n" +
                "                            \"value\":\"1\"\n" +
                "                        },\n" +
                "                        {\n" +
                "                            \"name\":\"备注说明\",\n" +
                "                            \"value\":\"1\"\n" +
                "                        },\n" +
                "                        {\n" +
                "                            \"name\":\"前三位\",\n" +
                "                            \"value\":\"1\"\n" +
                "                        },\n" +
                "                        {\n" +
                "                            \"name\":\"前五位\",\n" +
                "                            \"value\":\"1\"\n" +
                "                        },\n" +
                "                        {\n" +
                "                            \"name\":\"预计到货时间\",\n" +
                "                            \"value\":\"1\"\n" +
                "                        },\n" +
                "                        {\n" +
                "                            \"name\":\"SKU\",\n" +
                "                            \"value\":\"1\"\n" +
                "                        },\n" +
                "                        {\n" +
                "                            \"name\":\"对比供应商\",\n" +
                "                            \"value\":\"1\"\n" +
                "                        },\n" +
                "                        {\n" +
                "                            \"name\":\"电商物料描述\",\n" +
                "                            \"value\":\"1\"\n" +
                "                        },\n" +
                "                        {\n" +
                "                            \"name\":\"对比单价\",\n" +
                "                            \"value\":\"1\"\n" +
                "                        },\n" +
                "                        {\n" +
                "                            \"name\":\"对比采购提前期\",\n" +
                "                            \"value\":\"1\"\n" +
                "                        },\n" +
                "                        {\n" +
                "                            \"name\":\"保固期\",\n" +
                "                            \"value\":\"1\"\n" +
                "                        },\n" +
                "                        {\n" +
                "                            \"name\":\"对比保固期\",\n" +
                "                            \"value\":\"1\"\n" +
                "                        },\n" +
                "                        {\n" +
                "                            \"name\":\"参照行\",\n" +
                "                            \"value\":\"主行\"\n" +
                "                        }\n" +
                "                    ]\n" +
                "                }\n" +
                "            }\n" +
                "        ]\n" +
                "    }\n" +
                "}";
        String xmlstr = "<TableList>\n" +
                "<loginName>zhugenlan</loginName>\n" +
                "<tCode>FPC082</tCode>\n" +
                "<title>电商采购申请单-EBS-流水号</title>\n" +
                "<Table isson=\"1\">\n" +
                "<Field name=\"流水号\" value=\"PO1234455\"></Field>\n" +
                "<Field name=\"申请时间\" value=\"2021-12-29\"></Field>\n" +
                "<Field name=\"申请事业部\" value=\"采购管理中心\"></Field>\n" +
                "<Field name=\"采购供应商\" value=\"1\"></Field>\n" +
                "<Field name=\"币种\" value=\"CNY\"></Field>\n" +
                "<Field name=\"业务实体\" value=\"OU_SZPCB\"></Field>\n" +
                "<Field name=\"组织ID\" value=\"S01\"></Field>\n" +
                "</Table>\n" +
                "\n" +
                "<Table isson=\"2\">\n" +
                "<row>\n" +
                "<Field name=\"物料编码\"  value=\"1\"></Field>\n" +
                "<Field name=\"物料名称\" value=\"1\"></Field>\n" +
                "<Field name=\"物料类型\" value=\"1\"></Field>\n" +
                "<Field name=\"采购提前期\" value=\"1\"></Field>\n" +
                "<Field name=\"数量\" value=\"1\"></Field>\n" +
                "<Field name=\"单位\" value=\"1\"></Field>\n" +
                "<Field name=\"单价\" value=\"1\"></Field>\n" +
                "<Field name=\"价格对比\" value=\"1\"></Field>\n" +
                "<Field name=\"备注说明\" value=\"1\"></Field>\n" +
                "<Field name=\"前三位\" value=\"1\"></Field>\n" +
                "<Field name=\"前五位\" value=\"1\"></Field>\n" +
                "<Field name=\"预计到货时间\" value=\"1\"></Field>\n" +
                "<Field name=\"SKU\" value=\"1\"></Field>\n" +
                "<Field name=\"对比供应商\" value=\"1\"></Field>\n" +
                "<Field name=\"电商物料描述\" value=\"1\"></Field>\n" +
                "<Field name=\"对比单价\" value=\"1\"></Field>\n" +
                "<Field name=\"对比采购提前期\" value=\"1\"></Field>\n" +
                "<Field name=\"保固期\" value=\"1\"></Field>\n" +
                "<Field name=\"对比保固期\" value=\"1\"></Field>\n" +
                "<Field name=\"参照行\" value=\"主行\"></Field>\n" +
                "</row>\n" +
                "</Table>\n" +
                "</TableList>";

        System.out.println("JSON-->XML:");
        System.out.println("JSON:" + a);
        System.out.println("---------------------------------------------------------------");
        System.out.println("普通转XML带格式：\n" + StaxonUtils.json2xml(a));
        System.out.println("---------------------------------------------------------------");
        System.out.println("转XML去掉头部、前后补充<XML>：\n" + StaxonUtils.json2xmlPay(a));
        System.out.println("---------------------------------------------------------------");
        System.out.println("普通转XML去掉空格换行：\n" + StaxonUtils.json2xmlReplaceBlank(a));
        System.out.println("---------------------------------------------------------------");
        System.out.println("XML转JSON：\n" + StaxonUtils.xml2json(xmlstr));



    }

}
